﻿namespace QLPhongKham.BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDKhaiBaoTonKhoCT
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private int _dedongia;
        private long _deid;
        private int _desltondau;
        private int _iiddangnhap;
        private int _iidsanpham;
        private string _sngay;
        private string _sngaycn;
        private string _sSchema;
        private string _stenmaytinh;

        public dtoDKhaiBaoTonKhoCT()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDKhaiBaoTonKhoCT(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDKhaiBaoTonKhoCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1  and ", columnID, " = ", this._deid }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDKhaiBaoTonKhoCT.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDKhaiBaoTonKhoCT.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = Convert.ToInt32(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDKhaiBaoTonKhoCT.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDKhaiBaoTonKhoCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnIDSANPHAM, ",", columnSLTONDAU, ",", columnNGAY, ",", columnIDDANGNHAP, ",", columnTENMAYTINH, 
                    ",", columnDONGIA, ") values(", this._deid, ",", this._iidsanpham, ",", this._desltondau, ",to_timestamp('", this._sngay, "','dd/mm/yyyy hh24:mi'),", this._iiddangnhap, ",:", columnTENMAYTINH, ",", this._dedongia, 
                    ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 50).Value = this._stenmaytinh;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDKhaiBaoTonKhoCT.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0L;
            this._iidsanpham = 0;
            this._desltondau = 0;
            this._sngay = "";
            this._sngaycn = "";
            this._iiddangnhap = 0;
            this._dedongia = 0;
            this._stenmaytinh = Environment.MachineName;
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDKhaiBaoTonKhoCT.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnIDSANPHAM + "," + columnSLTONDAU + ",to_char(" + columnNGAY + ",'dd/mm/yyyy hh24:mi') as " + columnNGAY + ",to_char(" + columnNGAYCN + ",'dd/mm/yyyy hh24:mi:ss') as " + columnNGAYCN + "," + columnIDDANGNHAP + "," + columnTENMAYTINH + "," + columnDONGIA + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deid = Convert.ToInt64(dataTable.Rows[0][columnID].ToString());
                    this._iidsanpham = Convert.ToInt32(dataTable.Rows[0][columnIDSANPHAM].ToString());
                    this._desltondau = Convert.ToInt32(dataTable.Rows[0][columnSLTONDAU].ToString());
                    this._sngay = dataTable.Rows[0][columnNGAY].ToString();
                    this._sngaycn = dataTable.Rows[0][columnNGAYCN].ToString();
                    this._iiddangnhap = Convert.ToInt32(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                    this._stenmaytinh = dataTable.Rows[0][columnTENMAYTINH].ToString();
                    this._dedongia = Convert.ToInt32(dataTable.Rows[0][columnDONGIA].ToString());
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDKhaiBaoTonKhoCT.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDKhaiBaoTonKhoCT.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnIDSANPHAM + "," + columnSLTONDAU + ",to_char(" + columnNGAY + ",'dd/mm/yyyy hh24:mi') as " + columnNGAY + ",to_char(" + columnNGAYCN + ",'dd/mm/yyyy hh24:mi:ss') as " + columnNGAYCN + "," + columnIDDANGNHAP + "," + columnTENMAYTINH + "," + columnDONGIA + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDKhaiBaoTonKhoCT.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDKhaiBaoTonKhoCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnSLTONDAU, " = ", this._desltondau, ",", columnNGAY, " = to_timestamp('", this._sngay, "','dd/mm/yyyy hh24:mi'),", columnNGAYCN, " = now() ,", columnDONGIA, 
                    "=", this._dedongia, ",", columnIDDANGNHAP, "=", this._iiddangnhap, ",", columnTENMAYTINH, "=:", columnTENMAYTINH, " where ", columnID, " = ", this._deid, " and ", columnIDSANPHAM, 
                    " = ", this._iidsanpham
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 50).Value = this._stenmaytinh;
                return (command.ExecuteNonQuery() > -1);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDKhaiBaoTonKhoCT.cs");
                return false;
            }
        }

        public static string columnDONGIA
        {
            get
            {
                return "dongia";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnIDSANPHAM
        {
            get
            {
                return "idsanpham";
            }
        }

        public static string columnNGAY
        {
            get
            {
                return "ngay";
            }
        }

        public static string columnNGAYCN
        {
            get
            {
                return "ngaycn";
            }
        }

        public static string columnSLTONDAU
        {
            get
            {
                return "sltondau";
            }
        }

        public static string columnTENMAYTINH
        {
            get
            {
                return "tenmaytinh";
            }
        }

        public int pDONGIA
        {
            get
            {
                return this._dedongia;
            }
            set
            {
                this._dedongia = value;
            }
        }

        public long pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._iiddangnhap;
            }
            set
            {
                this._iiddangnhap = value;
            }
        }

        public int pIDSANPHAM
        {
            get
            {
                return this._iidsanpham;
            }
            set
            {
                this._iidsanpham = value;
            }
        }

        public string pNGAY
        {
            get
            {
                return this._sngay;
            }
            set
            {
                this._sngay = value;
            }
        }

        public string pNGAYCN
        {
            get
            {
                return this._sngaycn;
            }
            set
            {
                this._sngaycn = value;
            }
        }

        public int pSLTONDAU
        {
            get
            {
                return this._desltondau;
            }
            set
            {
                this._desltondau = value;
            }
        }

        public string pTENMAYTINH
        {
            get
            {
                return this._stenmaytinh;
            }
            set
            {
                this._stenmaytinh = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_khaibaotonkhoct";
            }
        }
    }
}

